Optional Vbufs.f

{
TODO:
   Fallbacks - 1) if can't map, do in client mem then copy; 2) if no vbo's use normal vertex arrays.
}

Requires Tools
Requires Quotations
Requires GL-Tools
Requires Kagi
Requires SORA\Vertices
Requires Dorothy.f

Only Forth Definitions

\ /Buffer Inherit
Node extend: vbuf
   vbo.pointer vb
   int.field vcount
   int.field ptype      \ GL primitive
   vertexdef.pointer vtype
end-type

node.make vbufs


method: vbuf.begin  ( count vbuf -- )
   vcount.!  vtype sizeof *  vb.map  to vpointer ;

: vbuf.end ( -- )
   vb.unbind ;



: (vertices)
   >r vbuf.begin r> execute vbuf.end ;

macro: vbuf.bind   ( vbuf -- )  vb.bind ;

: vbuf.init  ( vbuf -- )
   glGenBuffersARB( 1 &of swap.vb ) ;

\ -- Drawing --

method: vbuf.draw-some   ( index count vbuf -- )
   bind  0  vtype.glarrays
   ptype.@  -rot  glDrawArrays ;

method: vbuf.draw  ( vbuf -- )
   0 vcount.@ draw-some ;

\ -- Quotation words --

:[] vbuf.define:[   ( count vbuf xt -- )  ( -- )
   (vertices) ;

:[] vbuf.draw:[   ( count vbuf xt -- )  ( -- )
   over >q     (vertices)    q> draw  ;

\ -- Init --

: vbuf.make ( vertex-type gl-primitive  -- <name> )
   any.make   lastbody @ vbuf.<<   ptype.!  to vtype  >> ;

: init-vbufs
   vbufs.each:[ vbuf.init ;] ;

